package UDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class server {
    private DatagramSocket socket=null;

    public server(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    public void startup() throws IOException {
        System.out.println("Server is Running");
        while(true){
            DatagramPacket requirePacket=new DatagramPacket(new byte[4090],4090);
            socket.receive(requirePacket);
            String require=new String(requirePacket.getData(),0,requirePacket.getLength());
            String response=process(require);
            DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length,requirePacket.getSocketAddress());
            socket.send(responsePacket);
            System.out.printf("[%d:%s] req:%s res:%s\n",requirePacket.getPort(),requirePacket.getAddress(),require,response);
        }
    }

    public String process(String require) {
    return require;
    }

    public static void main(String[] args) throws IOException {
        server server=new server(9090);
        server.startup();
    }
}
